package com.mz.ieann.dokit.utils

import android.util.Xml
import androidx.annotation.NonNull
import java.io.File
import java.io.FileWriter
import java.io.IOException
import java.io.StringWriter
import java.util.concurrent.Executors

/**
 * <p>------------------------------------------------------
 * <p> Copyright (C) 2021 zhangmz, All rights reserved.
 * <p> If there is technical discussion, you can contact zhangmz90@foxmail.com
 * <p>------------------------------------------------------
 * <p> des
 * <p>
 *
 * @author Created by zhangmz
 * @date on 2021 2021/11/14 12:45
 */
object XmlWrite {

    /**
     * 写入dimen.xml文件内容，path为全路径
     */
    fun writeDimen(path : String){
        writeDimen(path, 1920, "dp")
    }
    fun writeDimen(path : String, suffix : String){
        writeDimen(path, 1920, suffix)
    }
    fun writeDimen(path : String, length : Int, suffix : String){
        var xmlContent = ""
        val executorService = Executors.newSingleThreadExecutor()
        executorService.execute {
            val serializer = Xml.newSerializer()
            val write = StringWriter()
            try {
                serializer.setOutput(write)
                serializer.startDocument("UTF-8", true)
                serializer.startTag("", "resources")
                for (i in 0..length){
                    serializer.startTag("", "dimen")
                    serializer.attribute("", "name", "${suffix}_$i")
                    serializer.text("$i$suffix")
                    serializer.endTag("", "dimen")
                }
                serializer.endTag("", "resources")
                serializer.endDocument()
                xmlContent = write.toString()
            }catch (e : Exception){
                LogUtils.e("error: $e")
            }finally {
                writeFile(xmlContent, path)
                if (!executorService.isShutdown){
                    executorService.shutdown()
                }
            }
        }
    }

    /**
     * 将any字符串写入path路径下
     */
    fun writeFile(any: String, path : String) : Boolean{
        val pair = checkPathLegal(path)
        if (!pair.first){
            return false
        }
        return write(any, path)
    }

    /**
     * 将any字符串写入path路径下
     */
    private fun write(any: String, path : String) : Boolean{
        var result = false
        var writer : FileWriter? = null
        try {
            writer = FileWriter(path, true)
            writer.write(any)
            result = true
        }catch (e : IOException){
            e.printStackTrace()
        }catch (e : RuntimeException){
            e.printStackTrace()
        }finally {
            try {
                writer?.close()
            }catch (e : Exception){
                e.printStackTrace()
            }
        }
        return result
    }

    /**
     * 验证路径的正确性，有则删除原有文件，无则创建，path不能是个文件夹
     */
    private fun checkPathLegal(path: String) : Pair<Boolean, String>{
        try {
            if (path.isEmpty()){
                return Pair(false, path)
            }
            val file = File(path)
            if (file.isDirectory){
                return Pair(false, path)
            }
            if (file.exists()){
                file.delete()
            }
            return Pair(file.createNewFile(), path)
        }catch (e : Exception){
            e.printStackTrace()
        }
        return Pair(true, path)
    }

}